package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.*;
import com.zoshine.zzg.pojo.*;
import com.zoshine.zzg.pojo.pojostatus.MenuStatus;
import com.zoshine.zzg.pojo.pojostatus.OrgType;
import com.zoshine.zzg.pojo.pojostatus.RelationSupplierPrisonStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 权限服务
 *
 * @author 贾俊峰
 * @time 2017/7/25 11:19
 */
@Service
public class PowerService extends BaseService<Power> {
    private PowerDao powerDao;
    private MenuDao menuDao;
    private RoleDao roleDao;
    private RelationSupplierPrisonDao relationSupplierPrisonDao;
    private OrganizationDao organizationDao;
    @Autowired
    public void setPowerDao(PowerDao powerDao) {
        this.powerDao = powerDao;
    }
    @Autowired
    public void setMenuDao(MenuDao menuDao) {
        this.menuDao = menuDao;
    }
    @Autowired
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }
    @Autowired
    public void setRelationSupplierPrisonDao(RelationSupplierPrisonDao relationSupplierPrisonDao) {
        this.relationSupplierPrisonDao = relationSupplierPrisonDao;
    }
    @Autowired
    public void setOrganizationDao(OrganizationDao organizationDao) {
        this.organizationDao = organizationDao;
    }

    /**
     * 获取用户菜单权限
     * @param user 用户
     */
    public List<Menu> getMenuPower(User user) {
        if (user.isSuperAdmin()) {
            Menu menu = new Menu();
            menu.setMenuStatus(MenuStatus.SHOW.getValue());
            return menuDao.selectList(menu);
        }
        Power power = new Power();
        power.setObjId(user.getRoleId());
        power.setObjType(Power.ObjType.ROLE);
        power.setPowerType(Power.PowerType.MENU);
        List<Power> roleMenuPower = powerDao.selectList(power);
        power.setObjType(Power.ObjType.USER);
        power.setObjId(user.getId());
        List<Power> userMenuPower = powerDao.selectList(power);

        // 去重
        Set<Long> menuPowerSet = new HashSet<>();
        for (Power p : roleMenuPower) {
            menuPowerSet.add(p.getPowerId());
        }
        for (Power p : userMenuPower) {
            menuPowerSet.add(p.getPowerId());
        }
        List<Long> idList = new ArrayList<>();
        idList.addAll(menuPowerSet);
        idList.add(0L);
        return menuDao.selectListByIdList(idList);
    }

    /**
     * 获取角色菜单权限
     * @param role 角色
     * @return 菜单权限
     */
    public List<Menu> getMenuPower(Role role) {
        Power power = new Power();
        power.setObjType(Power.ObjType.ROLE);
        power.setObjId(role.getId());
        power.setPowerType(Power.PowerType.MENU);
        List<Power> powerList = powerDao.selectList(power);
        List<Long> idList = new ArrayList<>();
        for (Power p : powerList) {
            idList.add(p.getPowerId());
        }
        if (idList.size() == 0) {
            return new ArrayList<>();
        }
        return menuDao.selectListByIdList(idList);
    }

    /**
     * 设置角色菜单权限
     * @param role 角色
     * @param menuPower 菜单权限
     */
    public void setMenuPower(Role role, String menuPower) {
        String[] idArr = menuPower.split(",");
        // 去重
        Set<String> set = new HashSet<>();
        set.addAll(Arrays.asList(idArr));
        List<Power> powerList = new ArrayList<>();
        Date now = new Date();
        for (String id : set) {
            Power power = new Power();
            power.setObjType(Power.ObjType.ROLE);
            power.setObjId(role.getId());
            power.setPowerType(Power.PowerType.MENU);
            power.setPowerId(Long.parseLong(id));
            power.setCreateTime(now);
            powerList.add(power);
        }
        cleanMenuPower(role);
        powerDao.batchInsert(powerList);
    }

    /**
     * 清除角色的菜单权限
     * @param role 角色
     */
    public void cleanMenuPower(Role role) {
        Power power = new Power();
        power.setObjType(Power.ObjType.ROLE);
        power.setObjId(role.getId());
        power.setPowerType(Power.PowerType.MENU);
        powerDao.delete(power);
    }

    /**
     * 获取用户的监所权限
     * @param user 用户
     */
    public List<Long> getPrisonPower(User user) {
        List<Long> organizationPower = new ArrayList<>();
        organizationPower.add(666L);

        if (user.isSuperAdmin()) { // 超级管理员
            Organization org = new Organization();
            org.setOrgType(OrgType.Prison.getValue());
            List<Organization> list = organizationDao.selectList(org);
//            org.setOrgType(OrgType.Detention_house.getValue());
//            List<Organization> list2 = organizationDao.selectList(org);
//            list.addAll(list2);
            for (Organization organization : list) {
                organizationPower.add(organization.getId());
            }
            return organizationPower;
        }

        Long orgId = user.getOrgId();
        Organization organization = organizationDao.selectByPrimaryKey(orgId);
        if (Objects.equals(organization.getOrgType(), OrgType.Prison.getValue())) {
            // 监所账户
            organizationPower.add(orgId);
        } else if (Objects.equals(organization.getOrgType(), OrgType.Supplier.getValue())) {
            // 供应商账户
            RelationSupplierPrison relationSupplierPrison = new RelationSupplierPrison();
            relationSupplierPrison.setStatus(RelationSupplierPrisonStatus.AUDITED.getValue());
            relationSupplierPrison.setSupplierId(orgId);
            List<RelationSupplierPrison> list = relationSupplierPrisonDao.selectList(relationSupplierPrison);
            for (RelationSupplierPrison relation : list) {
                organizationPower.add(relation.getPrisonId());
            }
        }
        return organizationPower;
    }

    /**
     * 获取用户的供应商权限
     * @param user 用户
     */
    public List<Long> getSupplierPower(User user) {
        List<Long> supplierPower = new ArrayList<>();
        supplierPower.add(666L);
        if (user.isSuperAdmin()) { // 超级管理员
            Organization org = new Organization();
            org.setOrgType(OrgType.Supplier.getValue());
            List<Organization> list = organizationDao.selectList(org);
            for (Organization organization : list) {
                supplierPower.add(organization.getId());
            }
            return supplierPower;
        }

        Long orgId = user.getOrgId();
        Organization org = organizationDao.selectByPrimaryKey(orgId);
        if (Objects.equals(org.getOrgType(), OrgType.Prison.getValue())) {
            // 监所账户
            RelationSupplierPrison relationSupplierPrison = new RelationSupplierPrison();
            relationSupplierPrison.setStatus(RelationSupplierPrisonStatus.AUDITED.getValue());
            relationSupplierPrison.setPrisonId(orgId);
            List<RelationSupplierPrison> list = relationSupplierPrisonDao.selectList(relationSupplierPrison);
            for (RelationSupplierPrison relation : list) {
                supplierPower.add(relation.getSupplierId());
            }
        } else if (Objects.equals(org.getOrgType(), OrgType.Supplier.getValue())) {
            // 供应商账户
            supplierPower.add(orgId);
        }
        return supplierPower;
    }
}
